﻿//对javascript的字符串增加一个trim函数
String.prototype.trim = function () {
	//   用正则表达式将前后空格
	//   用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)/g, "");
}


Array.prototype.clear = function () {
	this.length = 0;
}
Array.prototype.insertAt = function (index, obj) {
	this.splice(index, 0, obj);
}
Array.prototype.removeAt = function (index) {
	this.splice(index, 1);
}
Array.prototype.remove = function (obj) {
	var index = this.indexOf(obj);
	if (index >= 0) {
		this.removeAt(index);
	}
}


//javascript 日志输出到chrome的控制台上
function Log(str) {
	if (window.console && console.log) {
		console.log(str);
	}
}
//启动函数，清除Ajax缓存
$(document).ready(function () {
		$.ajaxSetup({
			cache: false //关闭AJAX相应的缓存
		});
		//将所有必填的字段都加上*标
	}
);


Utility = {};

Utility.GetQueryString = function (str) {
	var LocString = String(window.document.location.href);

	//alert(LocString);

	var rs = new RegExp("(^|)" + str + "=([^\&]*)(\&|$)", "gi").exec(LocString), tmp;
	if (tmp = rs)
		return tmp[2];
	return "没有这个参数";
};


//将以分钟为单位的时间间隔转换成时、分、秒的形式
Utility.descrTimeSpan = function (interval) {
	var strInterval = "";

	var hour = Math.floor(interval / 60);

	var min = Math.floor(interval - hour * 60);

	var sec = Math.floor((interval - hour * 60 - min) * 60);

	if (hour > 0)
		strInterval += hour + "小时,";
	if (min > 0)
		strInterval += min + "分,";
	if (sec > 0)
		strInterval += sec + "秒";
	return strInterval;
}

//将日期对话框，初始化为当期日期
$.fn.today = function (opt) {
	//alert(Date.today().toString("yyyy-MM-dd"));
	this.each(function () {
		this.value =
			Utility.today();
	});
};


$.fn.populateTemplate = function (data) {

	return this.each(function () {
		var template_content = $(this).html();

		$(this).setTemplate(template_content);
		$(this).processTemplate(data);
	});
};

Utility.initDatepicker = function () {
	//$(".datepicker").datepicker({ duration: 'fast' });  //对于日期输入框，进行日历对话框绑定

	//setter
	//$('.datepicker').datepicker('option', 'dayNamesMin', ['日', '一', '二', '三', '四', '五', '六']);

	$(".datepicker").click(function () {
		$(this).today();
	});

	$('.datepicker').attr("readonly", true);  //日期不能手动录入

	//alert("d");
};

//得到两个日期相差的天数
Utility.getDay = function (startTime, endTime) {
	var date1 = new Date(startTime.getFullYear(), startTime.getMonth(), startTime.getDate());
	var date2 = new Date(startTime.getFullYear(), endTime.getMonth(), endTime.getDate());
	var iDays = parseInt(Math.abs(date2 - date1) / 1000 / 60 / 60 / 24) + 1;

	return iDays;
}

Utility.addDay = function (date, day) {
	var ms = (day * 86400000);
	date.setMilliseconds(date.getMilliseconds() + ms);
	return date;
}

Utility.stringToDate = function (str) {
	return new Date(Date.parse(str.replace(/-/g, "/")));
}

Utility.getDate = function (dateObj) {
	var o = {
		"year": dateObj.getFullYear(),                 //月份
		"month": dateObj.getMonth() + 1,                 //月份
		"day": dateObj.getDate(),                    //日
		"hour": dateObj.getHours(),                   //小时
		"min": dateObj.getMinutes(),                 //分
		"sec": dateObj.getSeconds(),                 //秒
		"quarter": Math.floor((dateObj.getMonth() + 3) / 3), //季度
		"ms": dateObj.getMilliseconds()             //毫秒
	};

	o.month = o.month < 10 ? ("0" + o.month) : o.month;
	o.day = o.day < 10 ? ("0" + o.day) : o.day;
	o.hour = o.hour < 10 ? ("0" + o.hour) : o.hour;
	o.min = o.min < 10 ? ("0" + o.min) : o.min;
	o.sec = o.sec < 10 ? ("0" + o.sec) : o.sec;

	return o;
};

Utility.dateToString = function (_date, format) {
	//alert(format);

	var dt = this.getDate(_date);

	if (format == "dd hh:mm:ss")
		return dt.day + " " + dt.hour + ":" + dt.min + ":" + dt.sec;
	else if (format == "yyyy-MM-dd")
		return dt.year + "-" + dt.month + "-" + dt.day;
	else if (format == "hh:mm:ss")
		return dt.hour + ":" + dt.min + ":" + dt.sec;
	else if (format == "hh:mm")
		return dt.hour + ":" + dt.min;
	else if (format == "yyyy-MM-dd HH:mm")
		return dt.year + "-" + dt.month + "-" + dt.day + " " + dt.hour + ":" + dt.min;
	else
		return dt.year + "-" + dt.month + "-" + dt.day + " " + dt.hour + ":" + dt.min + ":" + dt.sec;
};

Utility.today = function (interval) {
	var dt = new Date();

	if (interval) {
		var ms = (interval * 86400000);
		dt.setMilliseconds(dt.getMilliseconds() + ms);
	}

	dt = this.dateToString(dt, "yyyy-MM-dd");
	return dt;
};

Utility.now = function (interval) {
	var dt = new Date();

	if (interval) {
		var ms = (interval * 86400000);
		dt.setMilliseconds(dt.getMilliseconds() + ms);
	}

	dt = this.dateToString(dt);
	return dt;
};


Utility.AddMonth = function (interval) {

	var dt = new Date();
	dt.setMonth(dt.getMonth() + interval)
	return dt.getYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate();
}

Utility.done = function () {
	$.unblockUI();
}

/*
 src 需保留的值
 pos 小数点后保留的位数
 */
Utility.getFormat = function (src, pos) {
	return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}

Utility.getFormat = function (src, pos) {
	return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}


